/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.purpose.controller;

import com.koron.common.Excel;
import com.koron.common.StaffAttribute;
import com.koron.common.bean.StaffBean;
import com.koron.common.permission.SPIAccountAnno;
import com.koron.purpose.bean.JobsTemplateBean;
import com.koron.purpose.service.api.JobsTemplateService;
import com.koron.purpose.utils.BusinessCode;
import com.koron.system.annotation.Log;
import com.koron.system.discrete.BusinessManipulation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.swan.bean.MessageBean;
import springfox.documentation.annotations.ApiIgnore;

import java.io.File;
import java.io.InputStream;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

    @Api(tags = "jobsTemplate", description = "岗位模板")
@RestController
@RequestMapping("/jobsTemplate")
public class JobsTemplateController {
    @Autowired
    private JobsTemplateService service;
    private final static Logger logger= LoggerFactory.getLogger(JobsTemplateController.class);

    @ApiOperation("删除")
    @ApiImplicitParam(name = "id", value = "id",  paramType = "query", dataType = "String")
    @RequestMapping(value = "/delete.htm", method = {RequestMethod.POST})
    @Log(title = "删除岗位模板", businessModule = "岗位模板", businessManipulation = BusinessManipulation.DELETE)
    public MessageBean<?> delete(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, String id) {
        if (null == user) {
            MessageBean<?> msg = new MessageBean<>();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return service.delete(id);
    }

    @ApiOperation("详情")
    @ApiImplicitParam(name = "id", value = "id",  paramType = "query", dataType = "String")
    @RequestMapping(value = "/details.htm", method = {RequestMethod.GET})
    public MessageBean<?> details(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, String id) {
        if (null == user) {
            MessageBean<?> msg = new MessageBean<>();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return service.details(id);
    }

    ;

    @ApiOperation("添加岗位模板")
    @RequestMapping(value = "/insert.htm", method = {RequestMethod.POST})
    @Log(title = "添加岗位模板", businessModule = "岗位模板", businessManipulation = BusinessManipulation.INSERT)
    public MessageBean<?> insert(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,
                                 @RequestBody JobsTemplateBean bean) {
        if (null == user) {
            MessageBean<?> msg = new MessageBean<>();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return service.insert(bean, user);
    }

    @ApiOperation("修改")
    @RequestMapping(value = "/update.htm", method = {RequestMethod.POST})
    @Log(title = "修改岗位模板", businessModule = "岗位模板", businessManipulation = BusinessManipulation.UPDATE)
    public MessageBean<?> update(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,
                                 @RequestBody JobsTemplateBean bean) {
        if (null == user) {
            MessageBean<?> msg = new MessageBean<>();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return service.update(bean, user);
    }

    @ApiOperation("查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "corpCode", value = "公司code",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "corpName", value = "公司名称",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "name", value = "岗位名称",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "code", value = "编码",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "jobType", value = "类型code",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "当前页",  paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数",  paramType = "query", dataType = "Integer")})
    @RequestMapping(value = "/query.htm", method = {RequestMethod.GET})
    public MessageBean<?> query(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @RequestParam(required = false) String corpCode, @RequestParam(required = false) String corpName, String name, String code, String jobType, Integer page, Integer pageSize) {
        if (null == user) {
            MessageBean<?> msg = new MessageBean<>();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return service.list(corpCode, corpName, name, code, jobType, page, pageSize);
    }

    @ApiOperation("获取岗位下拉list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "岗位名称",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "code", value = "编码",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "jobType", value = "类型code",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "当前页",  paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数",  paramType = "query", dataType = "Integer")})
    @RequestMapping(value = "/querySelectData.htm", method = {RequestMethod.GET})
    public MessageBean<?> querySelectData(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, String name, String code, String jobType, Integer page, Integer pageSize) {
        if (null == user) {
            MessageBean<?> msg = new MessageBean<>();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return service.querySelectData(name, code, jobType, page, pageSize);
    }

    @ApiOperation("岗位模板——证照矩阵(0内授权,1外授权，3没有证照)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "corpCode", value = "公司code",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "templateName", value = "岗位名称",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "当前页",  paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数",  paramType = "query", dataType = "Integer")})
    @RequestMapping(value = "/dutyTemplateCetificateMatrix.htm", method = {RequestMethod.GET})
    public MessageBean<?> dutyTemplateCetificateMatrix(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @RequestParam(required = false) final String corpCode, @RequestParam(required = false) final String templateName, @RequestParam Integer page, @RequestParam Integer pageSize) {
        if (null == user) {
            MessageBean<?> msg = new MessageBean<>();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        Integer start = null;
        if (page != null && page >= 0) {
            if (pageSize == null || pageSize <= 0) {
                pageSize = 15;
            }
            start = (page <= 1) ? 0 : (page - 1) * pageSize;
        }
        return service.dutyTemplateCetificateMatrix(corpCode, templateName, start, pageSize);
    }

    @ApiOperation("岗位模板——导出证照矩阵(0内授权,1外授权，3没有证照)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "corpCode", value = "公司code",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "templateName", value = "岗位名称",  paramType = "query", dataType = "String")
    })
    @RequestMapping(value = "/exportDutyTemplateCetificateMatrix.htm", method = {RequestMethod.GET})
    public HttpEntity<?> exportDutyTemplateCetificateMatrix(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @RequestParam(required = false) final String corpCode, @RequestParam(required = false) final String templateName) {
        if (null == user) {
            return null;
        }
        MessageBean<?> msg = service.dutyTemplateCetificateMatrix(corpCode, templateName, null, null);
        File targetFile = new File("static/excel/dutyCetificate.xlsx");
        LinkedHashMap<String, Object> map = new LinkedHashMap<>();
        if (null != msg.getData()) {
            map = (LinkedHashMap<String, Object>) msg.getData();
        }
        try {
            InputStream in = getClass().getClassLoader().getResourceAsStream("static/excel/dutyCetificate.xlsx");
            FileUtils.copyInputStreamToFile(in, targetFile);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return Excel.createFile("岗位模板-证照矩阵", targetFile, "dutyCetificate.btl", map);
    }

    @ApiOperation("岗位模板——危害因素矩阵")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "corpCode", value = "公司code",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "templateName", value = "岗位名称",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "当前页",  paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数",  paramType = "query", dataType = "Integer")})
    @RequestMapping(value = "/dutyTemplatehazardMatrix.htm", method = {RequestMethod.GET})
    public MessageBean<?> dutyTemplatehazardMatrix(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @RequestParam(required = false) String corpCode, String templateName, Integer page, Integer pageSize) {
        if (null == user) {
            MessageBean<?> msg = new MessageBean<>();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return service.dutyTemplatehazardMatrix(corpCode, templateName, page, pageSize);
    }

    @ApiOperation("岗位模板——导出危害因素矩阵")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "corpCode", value = "公司code",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "templateName", value = "岗位名称",  paramType = "query", dataType = "String")
    })
    @RequestMapping(value = "/exportDutyTemplatehazardMatrix.htm", method = {RequestMethod.GET})
    public HttpEntity<?> exportDutyTemplatehazardMatrix(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @RequestParam(required = false) final String corpCode, @RequestParam(required = false) final String templateName) {
        if (null == user) {
            return null;
        }
        MessageBean<LinkedHashMap> msg = service.exportDutyTemplatehazardMatrix(corpCode,templateName);
        File targetFile = new File("static/excel/dutyTrainingMatrix.xlsx");
        LinkedHashMap<String, Object> map = new LinkedHashMap<>();
        if (null != msg.getData()) {
            map = msg.getData();
        }
        try {
            InputStream in = getClass().getClassLoader().getResourceAsStream("static/excel/dutyTemplatehazardMatrix.xlsx");
            FileUtils.copyInputStreamToFile(in, targetFile);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return Excel.createFile("岗位模板-危险因素矩阵", targetFile, "dutyTemplatehazardMatrix.btl", map);
    }

    @ApiOperation("导入岗位模板")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "导入excel文件",  paramType = "query", dataType = "String")
    })
    @RequestMapping(value = "/importExcel.htm", method = {RequestMethod.POST})
    public MessageBean<String> importExcel(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, MultipartFile file) {
        return service.importExcel(user, file);
    }

    @ApiOperation("下载导入岗位的模板")
    @RequestMapping(value = "/exportTemplate.htm", method = RequestMethod.GET)
    public HttpEntity<?> exportTemplate() {
        Map<String, Object> map = new HashMap<>();
        File targetFile = new File("static/excel/jobsTemplateBeanTemplate.xlsx");
        try {
            InputStream in = getClass().getClassLoader().getResourceAsStream("static/excel/jobsTemplateBeanTemplate.xlsx");
            FileUtils.copyInputStreamToFile(in, targetFile);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            //logger.error(e.getMessage(),e);
        }
        return Excel.createFile("导入岗位模板", targetFile, "default.btl", map);
    }

}
